<?php
session_start();
require_once '../includes/config.php';
require_once '../includes/database.php';
require_once '../includes/auth.php';
require_once '../includes/attendance.php';

// 检查权限
$auth = getAuth();
$auth->requireLogin();

// 获取当前用户信息
$currentUser = $auth->getCurrentUser();

// 获取考勤实例
$attendance = getAttendance();

// 获取今天的考勤记录
$todayRecord = $attendance->getTodayRecord($currentUser['id']);

// 获取本月考勤统计
$currentYear = date('Y');
$currentMonth = date('m');
$monthlyReport = $attendance->getMonthlyReport($currentUser['id'], $currentYear, $currentMonth);

// 获取最近考勤记录
$recentRecords = $attendance->getUserRecords(
    $currentUser['id'], 
    date('Y-m-01'), 
    date('Y-m-d'),
    10
);
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户仪表板 - 考勤管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .sidebar {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            min-height: 100vh;
            position: fixed;
            width: 250px;
        }
        .sidebar .nav-link {
            color: white;
            padding: 0.75rem 1rem;
            border-radius: 0.375rem;
            margin: 0.125rem 0.5rem;
        }
        .sidebar .nav-link:hover, .sidebar .nav-link.active {
            background: rgba(255,255,255,0.1);
        }
        .sidebar .nav-link i {
            width: 20px;
            text-align: center;
            margin-right: 0.5rem;
        }
        .main-content {
            margin-left: 250px;
            padding: 20px;
        }
        .stat-card {
            border-radius: 10px;
            border: none;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
            transition: transform 0.2s;
        }
        .stat-card:hover {
            transform: translateY(-2px);
        }
        .stat-icon {
            font-size: 2rem;
            opacity: 0.7;
        }
        .navbar-custom {
            background: white;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .clock-btn {
            font-size: 1.2rem;
            padding: 1rem 2rem;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <!-- 侧边栏 -->
    <div class="sidebar">
        <div class="p-3 text-center border-bottom">
            <h4 class="mb-0">
                <i class="fas fa-clock me-2"></i>
                考勤系统
            </h4>
            <small>用户面板</small>
        </div>
        
        <nav class="nav flex-column p-2">
            <a href="dashboard.php" class="nav-link active">
                <i class="fas fa-tachometer-alt"></i>
                仪表板
            </a>
            <a href="attendance.php" class="nav-link">
                <i class="fas fa-clipboard-list"></i>
                我的考勤
            </a>
            <a href="profile.php" class="nav-link">
                <i class="fas fa-user"></i>
                个人信息
            </a>
            <div class="mt-auto p-2">
                <a href="../public/logout.php" class="nav-link text-warning">
                    <i class="fas fa-sign-out-alt"></i>
                    退出登录
                </a>
            </div>
        </nav>
    </div>

    <!-- 主内容区 -->
    <div class="main-content">
        <!-- 顶部导航 -->
        <nav class="navbar navbar-expand navbar-custom mb-4">
            <div class="container-fluid">
                <div class="navbar-nav me-auto">
                    <span class="navbar-text">
                        <i class="fas fa-home me-2"></i>用户仪表板
                    </span>
                </div>
                <div class="navbar-nav">
                    <span class="navbar-text me-3">
                        <i class="fas fa-user me-2"></i>
                        <?php echo htmlspecialchars($currentUser['full_name']); ?>
                        (<?php echo htmlspecialchars($currentUser['department']); ?>)
                    </span>
                    <a href="profile.php" class="nav-link">
                        <i class="fas fa-cog"></i> 个人设置
                    </a>
                </div>
            </div>
        </nav>

        <!-- 打卡区域 -->
        <div class="row mb-4">
            <div class="col-12">
                <div class="card">
                    <div class="card-header bg-primary text-white">
                        <h5 class="card-title mb-0">
                            <i class="fas fa-clock me-2"></i>今日打卡
                        </h5>
                    </div>
                    <div class="card-body text-center">
                        <div class="row align-items-center">
                            <div class="col-md-4">
                                <h4><?php echo date('Y年m月d日'); ?></h4>
                                <h2 id="current-time"><?php echo date('H:i:s'); ?></h2>
                                <small class="text-muted">当前时间</small>
                            </div>
                            <div class="col-md-4">
                                <div class="mb-3">
                                    <strong>上午上班:</strong>
                                    <div class="h5"><?php echo $todayRecord['morning_clock_in'] ?? '--:--:--'; ?></div>
                                </div>
                                <div class="mb-3">
                                    <strong>下午上班:</strong>
                                    <div class="h5"><?php echo $todayRecord['afternoon_clock_in'] ?? '--:--:--'; ?></div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <?php if (!$todayRecord || (!$todayRecord['morning_clock_in'] && !$todayRecord['afternoon_clock_in'])): ?>
                                    <form method="post" action="clock_action.php">
                                        <input type="hidden" name="action" value="clock_in">
                                        <button type="submit" class="btn btn-success btn-lg clock-btn w-100">
                                            <i class="fas fa-sign-in-alt me-2"></i>上班打卡
                                        </button>
                                    </form>
                                <?php elseif (!$todayRecord['afternoon_clock_out']): ?>
                                    <form method="post" action="clock_action.php">
                                        <input type="hidden" name="action" value="clock_out">
                                        <button type="submit" class="btn btn-warning btn-lg clock-btn w-100">
                                            <i class="fas fa-sign-out-alt me-2"></i>下班打卡
                                        </button>
                                    </form>
                                <?php else: ?>
                                    <div class="alert alert-success">
                                        <i class="fas fa-check-circle me-2"></i>
                                        今日打卡已完成
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 统计卡片 -->
        <div class="row mb-4">
            <div class="col-md-3 mb-3">
                <div class="card stat-card border-primary">
                    <div class="card-body">
                        <div class="d-flex justify-content-between">
                            <div>
                                <h5 class="card-title text-primary">本月出勤</h5>
                                <h2 class="text-primary"><?php echo $monthlyReport['stats']['work_days'] ?? 0; ?></h2>
                                <p class="card-text text-muted">工作日</p>
                            </div>
                            <div class="stat-icon text-primary">
                                <i class="fas fa-calendar-check"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-md-3 mb-3">
                <div class="card stat-card border-warning">
                    <div class="card-body">
                        <div class="d-flex justify-content-between">
                            <div>
                                <h5 class="card-title text-warning">迟到次数</h5>
                                <h2 class="text-warning"><?php echo $monthlyReport['stats']['late_days'] ?? 0; ?></h2>
                                <p class="card-text text-muted">本月累计</p>
                            </div>
                            <div class="stat-icon text-warning">
                                <i class="fas fa-clock"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-md-3 mb-3">
                <div class="card stat-card border-danger">
                    <div class="card-body">
                        <div class="d-flex justify-content-between">
                            <div>
                                <h5 class="card-title text-danger">缺勤天数</h5>
                                <h2 class="text-danger"><?php echo $monthlyReport['stats']['absent_days'] ?? 0; ?></h2>
                                <p class="card-text text-muted">本月累计</p>
                            </div>
                            <div class="stat-icon text-danger">
                                <i class="fas fa-user-times"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-md-3 mb-3">
                <div class="card stat-card border-success">
                    <div class="card-body">
                        <div class="d-flex justify-content-between">
                            <div>
                                <h5 class="card-title text-success">平均工时</h5>
                                <h2 class="text-success"><?php echo $monthlyReport['stats']['avg_hours'] ?? 0; ?></h2>
                                <p class="card-text text-muted">小时/天</p>
                            </div>
                            <div class="stat-icon text-success">
                                <i class="fas fa-chart-line"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <!-- 本月统计 -->
            <div class="col-md-6 mb-4">
                <div class="card">
                    <div class="card-header bg-info text-white">
                        <h5 class="card-title mb-0">
                            <i class="fas fa-chart-pie me-2"></i>本月考勤统计
                        </h5>
                    </div>
                    <div class="card-body">
                        <div class="row text-center">
                            <div class="col-4">
                                <h3 class="text-success"><?php echo $monthlyReport['stats']['normal_days'] ?? 0; ?></h3>
                                <small class="text-muted">正常</small>
                            </div>
                            <div class="col-4">
                                <h3 class="text-warning"><?php echo $monthlyReport['stats']['late_days'] ?? 0; ?></h3>
                                <small class="text-muted">迟到</small>
                            </div>
                            <div class="col-4">
                                <h3 class="text-danger"><?php echo $monthlyReport['stats']['absent_days'] ?? 0; ?></h3>
                                <small class="text-muted">缺勤</small>
                            </div>
                        </div>
                        <hr>
                        <div class="row text-center">
                            <div class="col-6">
                                <h4 class="text-primary"><?php echo $monthlyReport['stats']['total_hours'] ?? 0; ?></h4>
                                <small class="text-muted">总工时</small>
                            </div>
                            <div class="col-6">
                                <h4 class="text-secondary"><?php echo $monthlyReport['stats']['work_days'] ?? 0; ?></h4>
                                <small class="text-muted">出勤天数</small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 快速操作 -->
            <div class="col-md-6 mb-4">
                <div class="card">
                    <div class="card-header bg-success text-white">
                        <h5 class="card-title mb-0">
                            <i class="fas fa-bolt me-2"></i>快速操作
                        </h5>
                    </div>
                    <div class="card-body">
                        <div class="d-grid gap-2">
                            <a href="attendance.php" class="btn btn-outline-primary">
                                <i class="fas fa-history me-2"></i>查看考勤记录
                            </a>
                            <a href="calendar.php" class="btn btn-outline-success">
                                <i class="fas fa-calendar-alt me-2"></i>考勤日历
                            </a>
                            <a href="profile.php" class="btn btn-outline-warning">
                                <i class="fas fa-user-edit me-2"></i>修改个人信息
                            </a>
                            <a href="attendance.php?export=1" class="btn btn-outline-info">
                                <i class="fas fa-download me-2"></i>导出考勤记录
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 最近考勤记录 -->
        <div class="card">
            <div class="card-header">
                <h5 class="card-title mb-0">
                    <i class="fas fa-history me-2"></i>最近考勤记录
                </h5>
            </div>
            <div class="card-body">
                <?php if (empty($recentRecords)): ?>
                    <div class="text-center text-muted py-4">
                        <i class="fas fa-inbox fa-3x mb-3"></i>
                        <p>暂无考勤记录</p>
                    </div>
                <?php else: ?>
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>日期</th>
                                    <th>上班时间</th>
                                    <th>下班时间</th>
                                    <th>状态</th>
                                    <th>工作时长</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($recentRecords as $record): ?>
                                <tr>
                                    <td><?php echo $record['record_date']; ?></td>
                                    <td><?php echo $record['clock_in'] ?: '--'; ?></td>
                                    <td><?php echo $record['clock_out'] ?: '--'; ?></td>
                                    <td>
                                        <?php
                                        $statusLabels = [
                                            'normal' => '<span class="badge bg-success">正常</span>',
                                            'late' => '<span class="badge bg-warning">迟到</span>',
                                            'early_leave' => '<span class="badge bg-warning">早退</span>',
                                            'absent' => '<span class="badge bg-danger">缺勤</span>',
                                            'leave' => '<span class="badge bg-info">请假</span>',
                                            'business_trip' => '<span class="badge bg-primary">出差</span>'
                                        ];
                                        echo $statusLabels[$record['status']] ?? '<span class="badge bg-secondary">未知</span>';
                                        ?>
                                    </td>
                                    <td><?php echo $record['work_hours'] ? $record['work_hours'] . '小时' : '--'; ?></td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 更新时间显示
        function updateTime() {
            const now = new Date();
            const timeString = now.toLocaleTimeString('zh-CN');
            document.getElementById('current-time').textContent = timeString;
        }
        
        setInterval(updateTime, 1000);
        updateTime();
    </script>
</body>
</html>
